package sketchpad.gui.drawinggraph;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import sketchpad.constant.CHNName;
import sketchpad.constant.Config;
import sketchpad.constant.Geometry;
import sketchpad.constant.SketchPadColor;
import sketchpad.gui.drawinggraph.node.Node;

import java.io.Serializable;

public class Rectangle extends DrawItem implements Serializable {

    public Rectangle(int id, Node node){
        super(id, node);
        setGeometryClass(Geometry.RECTANGLE);
        setLabel(CHNName.RECTANGLE);
    }

    @Override
    public void draw(Node mouseNode, GraphicsContext gc){
        double x1 = getLeftUpNode().x;
        double y1 = getLeftUpNode().y;
        double x2 = mouseNode.x;
        double y2 = mouseNode.y;
        setRightDownNode(new Node(x2,y2));
        if (getId()== Config.SELECT_ID){
            gc.setFill(Color.valueOf(SketchPadColor.COLOR_RECTANGLE_SELECT_BOX));
            gc.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
        }else {
            if (!isSelect())gc.setStroke(Color.valueOf(SketchPadColor.COLOR_RECTANGLE));
            gc.strokeLine(x1,y1,x2,y1);
            gc.strokeLine(x2,y1,x2,y2);
            gc.strokeLine(x2,y2,x1,y2);
            gc.strokeLine(x1,y2,x1,y1);
        }
    }
}
